//
//  DownloadingCell.m
//  To Feel
//
//  Created by 孙嘉胤 on 16/7/22.
//  Copyright © 2016年 caoyu. All rights reserved.
//

#import "SJ_DownloadingCell.h"
#import "Masonry.h"
@implementation SJ_DownloadingCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self createView];
        
    }
    return self;
    
    
}

- (void)createView {
    
    self.fileNameLabel = [[UILabel alloc]init];
    self.progressLabel = [[UILabel alloc]init];
    self.progress = [[UIProgressView alloc]init];
    self.speedLabel = [[UILabel alloc]init];
    self.downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    
    [self.contentView addSubview:self.fileNameLabel];
    [self.contentView addSubview:self.progress];
    [self.contentView addSubview:self.progressLabel];
    [self.contentView addSubview:self.speedLabel];
    [self.contentView addSubview:self.downloadBtn];
    
    // Initialization code
    self.downloadBtn.clipsToBounds = true;
    [self.downloadBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.downloadBtn setTitle:@"🕘" forState:UIControlStateNormal];
    [self.downloadBtn setTitle:@"↓" forState:UIControlStateSelected];
    
    
    [self.downloadBtn addTarget:self action:@selector(clickDownload:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)layoutSubviews {
    
    
    [self.progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(20);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.right.equalTo(self.downloadBtn.mas_left).with.offset(-5);
    }];
    [self.fileNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(20);
        make.bottom.equalTo(self.progress.mas_top).with.offset(-6);
        make.size.mas_equalTo(CGSizeMake(300, 30));
        
    }];
    
    [self.progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.fileNameLabel.mas_left);
        make.top.equalTo(self.progress.mas_bottom).with.offset(6);
        make.size.mas_equalTo(CGSizeMake(300, 30));
    }];
    
    [self.speedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.progress.mas_right);
        make.top.equalTo(self.progress.mas_bottom).with.offset(6);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    
    [self.downloadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).with.offset(-2);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.left.equalTo(self.progress.mas_right).with.offset(5);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

/**
 *  暂停、下载
 *
 *  @param sender UIButton
 */
- (void)clickDownload:(UIButton *)sender {
        sender.selected = !sender.selected;
    if (self.downloadBlock) {
        self.downloadBlock(sender);
    }
}

/**
 *  model setter
 *
 *  @param sessionModel sessionModel
 */
- (void)setSessionModel:(ZFSessionModel *)sessionModel
{
    _sessionModel = sessionModel;
    self.fileNameLabel.text = sessionModel.fileName;
    NSUInteger receivedSize = ZFDownloadLength(sessionModel.url);
    NSString *writtenSize = [NSString stringWithFormat:@"%.2f %@",
                             [sessionModel calculateFileSizeInUnit:(unsigned long long)receivedSize],
                             [sessionModel calculateUnit:(unsigned long long)receivedSize]];
    CGFloat progress = 1.0 * receivedSize / sessionModel.totalLength;
    self.progressLabel.text = [NSString stringWithFormat:@"%@/%@ (%.2f%%)",writtenSize,sessionModel.totalSize,progress*100];
    self.progress.progress = progress;
    self.speedLabel.text = @"已暂停";
}


@end
